<?php

namespace app\api\controller;

use think\Db;
use app\common\controller\Logined;
use app\common\service\BalanceService;
// use app\common\controller\UnLogin;
use app\common\service\TranslateService;
class Robot extends Logined {

    //质押数据
    public function config(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $wid = 1;
        // $coin   = model('WalletModel')->getSingleWallet($wid);
        // $coin['amount'] = model('UserBalanceModel')->getBalance($uid,$wid);

        $data = Db::table('tp_invest_config')->where('id','>',1)->where('status',1)->where('is_open',1)->select();
        foreach ($data as $key => &$val) {
            // $val['rate'] = round($val['rate']*100,2).'%';
            $val['rate'] = $val['rate']/1;
            //翻译
            $data[$key]['name'] = TranslateService::baiduApi($val['name'],$this->lang);
        }
        // $data = [];
        // $data['coin'] = $coin;
        // $data['config'] = $config;
        $this->retData(0,'success',$data);
    }

    //质押下单
    public function order(){
        $uid = $this->uid;
        $amount = P('amount')?:100;
        $paypass = P('paypass');
        $id = P('id')?: 1;
        $code = P('code') ?: '';
        
        if (empty($uid) || empty($amount) || empty($paypass) || empty($id)) {
            $this->retData(1, '缺少参数');
        }
        Db::startTrans();

        $date = date('Y-m-d H:i:s');
        $res1 = $res2 = $res3 = $res4 = $res5 = 1;
        $config = Db::table('tp_invest_config')->where('id',$id)->lock(true)->find();
        if (empty($config)) {
            $this->retData(1, '配置有误');
        }
        if ($paypass != $this->user_info['paypass']) {
            $this->retData(1, '交易密码错误');
        }
        //
        if ($id <= $this->user_info['robot_level']) {
            $this->retData(1, '请购买更高等级的智能云服务器');
        }

        //补差价升级
        $need_balance = $config['price'];
        if ($this->user_info['level'] > 0) {
            $has_balance = Db::table('tp_invest_record')->where('uid',$uid)->sum('cost')?:0;
            $need_balance = $config['price'] - $has_balance;
        }
        
        $type = 2;

        //兑换码或优惠卷
        if ($code) {
            $code_config = Db::table('tp_invest_code')->where('code',$code)->find();
            if (empty($code_config)) {
                $this->retData(1, '兑换码或优惠卷有误');
            }
            // if ($code_config['status'] != 2) {
            //     $this->retData(1, '兑换码或优惠卷不可用');
            // }
            if ($code_config['uid'] != 0 || $code_config['status'] != 2) {
                $this->retData(1, '兑换码或优惠卷不可用');
            }
            
            if ($code_config['type'] == 1) {
                //兑换码
                if ($code_config['robot'] != 0 && $code_config['robot'] != $id) {
                    $this->retData(1, '非该等级的智能云服务器兑换码');
                }
                $need_balance = 0;
                $type = 3;
            } else if ($code_config['type'] == 2) {
                //优惠卷
                if ($code_config['amount'] >= $need_balance) {
                    $this->retData(1, '优惠卷金额需小于实际支付数量');
                }
                $need_balance = $need_balance - $code_config['amount'];
                $type = 4;
            }
        }
        
        $wid = 1;
        //质押
        Db::table('tp_invest_record')->where('uid',$uid)->update(['status'=>3]);
        $res1 = Db::table('tp_invest_record')->insertGetId([
            'orderno'=>date('YmdHis'),
            'name'=>$config['name'],
            'uid'=>$uid,
            'wid'=>$wid,
            'cost'=>$need_balance,
            'price'=>$config['price'],
            'days'=>$config['days'],
            'rate'=>$config['rate'],
            'config_id'=>$config['id'],
            'order_date'=>$date,
            'status'=>1,
            'type'=>$type,
        ]);
        
        if ($code) {
            Db::table('tp_invest_code')->where('id',$code_config['id'])->update([
                'uid'=>$uid,
                'status'=>3,
                'order_id'=>$res1,
                'use_date'=>$date,
                'remark'=>$config['name']
            ]); 
        }

        if ($need_balance > 0) {
            
            //是否有体验金
            $ty_balance = model('UserBalanceModel')->getBalance($uid,10);
            if ($ty_balance > 0) {
                if ($ty_balance >= $need_balance) {
                    BalanceService::updateBalance($uid,10,-$need_balance,'购买智能云服务器',101,$res1);
                    $need_balance = 0;
                } else {
                    BalanceService::updateBalance($uid,10,-$ty_balance,'购买智能云服务器',101,$res1);
                    $need_balance = $need_balance - $ty_balance;
                }
            }
            if ($need_balance > 0) {
                $balance = model('UserBalanceModel')->getBalance($uid,$wid);
                if ($balance < $need_balance) {
                    $this->retData(1, '余额不足');
                }
                $res2 = BalanceService::updateBalance($uid,$wid,-$need_balance,'购买智能云服务器',101,$res1);
            }  
        }
        
        $res3 = Db::table('tp_invest_config')->where('id',$id)->setInc('has',1);
        $res4 = Db::table('tp_user')->where('id',$uid)->update(['robot_level'=>$id]);
        
        $balance = model('UserBalanceModel')->getBalance($uid,1);
        $ty_balance = model('UserBalanceModel')->getBalance($uid,10);
        $balance = $balance + $ty_balance;
        
        //直推獎勵
        if ($need_balance > 0) {
            $res5 = $this->inviteProfit($uid,$need_balance,$res1);
        }
        if ($balance >= $config['min']) {
            $this->open2($uid,$res1);
            // Db::table('tp_invest_record')->where('id',$res1)->update(['status'=>2]);
        }
       Db::table('tp_user')->where('id',$uid)->update([
            'static_profit_status'=>1,
            'invite_profit_status'=>1,
            'team_profit_status'=>1
       ]); 
        if($res1 && $res2 && $res3 && $res4 && $res5){
            Db::commit();
            $this->retData(0,'购买成功,请及时开启获取收益');
        } else {
            Db::rollback();
            $this->retData(1,'购买失败');
        }
    }

    public function inviteProfit($uid,$profit,$order_id){
        if ($profit <= 0) {
            return true;
        }
        $date = date('Y-m-d H:i:s');
        $user_info = Db::table('tp_user')->where('id',$uid)->find();
        for ($i=1; $i < 5; $i++) { 
            $user_info = Db::table('tp_user')->where('id',$user_info['pid'])->find();
            if ($user_info) {
                if ($user_info['invite_profit_status'] != 1) {
                    continue;
                }
                switch ($i) {
                    case 1:
                        $rate_name = 'one';
                        break;
                    case 2:
                        $rate_name = 'two';
                        break;
                    case 3:
                        $rate_name = 'three';
                        break;
                    case 4:
                        $rate_name = 'four';
                        break;
                    default:
                        # code...
                        break;
                }
                if ($user_info['level']<3) {
                    $rate = Db::table('tp_invest_level_config')->where('level',$user_info['level'])->value($rate_name);
                    $invite_profit = $rate * $profit;
                    if ($invite_profit > 0) {
                        BalanceService::updateBalance($user_info['id'],1,$invite_profit,$i.'级推广佣金',103,$order_id);
                        Db::table('tp_invest_profit_record')->insert([
                            'uid'=>$user_info['id'],
                            'x_uid'=>$uid,
                            'order_id'=>$order_id,
                            'profit'=>$invite_profit,
                            'type'=>20,
                            'wid'=>1,
                            'remark'=>'销售奖励',
                            'date'=>$date
                        ]);
                    }
                    
                } else if ($user_info['level'] == 3) {
                    $rate1 = Db::table('tp_invest_level_config')->where('id',4)->value($rate_name);
                    
                    $invite_profit1 = $rate1 * $profit;
                    if ($invite_profit1 > 0) {
                        BalanceService::updateBalance($user_info['id'],1,$invite_profit1,$i.'级推广佣金',103,$order_id);
                        Db::table('tp_invest_profit_record')->insert([
                            'uid'=>$user_info['id'],
                            'x_uid'=>$uid,
                            'order_id'=>$order_id,
                            'profit'=>$invite_profit1,
                            'type'=>20,
                            'wid'=>1,
                            'remark'=>'销售奖励',
                            'date'=>$date
                        ]);
                    }
                    

                    $rate2 = Db::table('tp_invest_level_config')->where('id',5)->value($rate_name);
                    $invite_profit2 = $rate2 * $profit;
                    if ($invite_profit2 > 0) {
                        BalanceService::updateBalance($user_info['id'],11,$invite_profit2,$i.'级城市合伙人推广佣金',105,$order_id);
                        Db::table('tp_invest_profit_record')->insert([
                            'uid'=>$user_info['id'],
                            'x_uid'=>$uid,
                            'order_id'=>$order_id,
                            'profit'=>$invite_profit2,
                            'type'=>21,
                            'wid'=>11,
                            'remark'=>'城市合伙人销售奖励',
                            'date'=>$date
                        ]);
                    }
                    

                } else if ($user_info['level'] == 4) {
                    $rate1 = 0;
                    $rate2 = Db::table('tp_invest_level_config')->where('id',6)->value($rate_name);
                    $invite_profit2 = $rate2 * $profit;
                    if ($invite_profit2 > 0) {
                        BalanceService::updateBalance($user_info['id'],12,$invite_profit2,$i.'级营销经理销售奖励',106,$order_id);
                        Db::table('tp_invest_profit_record')->insert([
                            'uid'=>$user_info['id'],
                            'x_uid'=>$uid,
                            'order_id'=>$order_id,
                            'profit'=>$invite_profit2,
                            'type'=>22,
                            'remark'=>'营销经理销售奖励',
                            'wid'=>12,
                            'date'=>$date
                        ]);
                    }
                }
            } else {
                return true;
            }
        }
        return true;
    }
    public function open2($uid,$order_id){
        $res1 = $res2 = $res3 = $res4 = $res5 = 1;
        Db::startTrans();
        $record = Db::table('tp_invest_record')->where('uid',$uid)->where('id',$order_id)->find();
        if (empty($record)) {
            return false;
        }
        if ($record['close_type'] == 1) {
            if ((strtotime($record['close_date']) + 86400) > time()) {
                return false;
            }
        }
        if ($record['close_type'] == 2) {
            if ((strtotime($record['close_date']) + 86400*2) > time()) {
                return false;
            }
        }
        
        $config = Db::table('tp_invest_config')->where('id',$record['config_id'])->find();
        $min = $config['min'];
        $balance = model('UserBalanceModel')->getBalance($uid,1);
        $ty_balance = model('UserBalanceModel')->getBalance($uid,10);

        $balance = $balance + $ty_balance;
        if ($balance < $min) {
            return false;
        }
        $res1 = Db::table('tp_invest_record')->where('id',$record['id'])->update(['status'=>2,'open_date'=>date('Y-m-d H:i:s'),'open_type'=>1]);
        //自身升级
        if ($record['config_id'] >= 4) {
            if ($this->user_info['level'] == 1) {
                $res2 = Db::table('tp_user')->where('id',$uid)->update(['level'=>2,'vip_update_date'=>date('Y-m-d H:i:s')]);
            }
        }

        //上级升级
        $is_first = Db::table('tp_invest_record')->where('uid',$uid)->where('open_type','>',0)->where('config_id','>',1)->count();
        if ($is_first == 1) {
            $pid = $this->user_info['pid'];
            if ($pid != 0) {
                $p_userinfo = Db::table('tp_user')->where('id',$pid)->find();
                if ($p_userinfo['level'] < 3) {
                    $p_config_id = Db::table('tp_invest_record')->where('uid',$pid)->where('status',2)->value('config_id');
                    if ($p_config_id >= 4) {
                        $p_level = 2;
                    } else {
                        $p_level = 1; 
                    }
         
                    if ($p_userinfo['vip_next_days'] >= 10) {
                        $vip_next_days = 10;
                    } else {
                        $vip_next_days = $p_userinfo['vip_next_days'] + 1;
                    }

                    if ($p_userinfo['level'] >= 1) {
                            $res3 = Db::table('tp_user')->where('id',$pid)->update([
                                'level'=>$p_level,
                                'vip_date'=>date('Y-m-d H:i:s',strtotime($p_userinfo['vip_date'])+86400*$vip_next_days),
                                'vip_next_days'=>$vip_next_days,
                                'vip_update_date'=>date('Y-m-d H:i:s')
                            ]);
                        } else {
                            $res3 = Db::table('tp_user')->where('id',$pid)->update([
                                'level'=>$p_level,
                                'vip_date'=>date('Y-m-d H:i:s',strtotime('+5 days')),
                                'vip_next_days'=>6,
                                'vip_update_date'=>date('Y-m-d H:i:s')
                            ]);
                        }

                    //上级体验金
                    $num = Db::table('tp_invest_record')->alias('a')
                        ->join('tp_user b','b.id=a.uid and b.pid='.$pid)
                        ->where('a.config_id','>',1)
                        ->where('a.status','in','2,3,4')
                        ->group('a.uid')
                        ->count();
                    if ($num >= 3) {
                        $ty_balance = model('UserBalanceModel')->getBalance($pid,10);
                        if ($ty_balance > 0 ) {
                            if ($ty_balance > 10) {
                                $ty_balance = 10;
                            }
                            $res4 = BalanceService::updateBalance($pid,10,-$ty_balance,'体验金转出',91);
                            $res5 = BalanceService::updateBalance($pid,1,$ty_balance,'体验金转入',92);
                        }  
                    }
                }
            }
        }
        if($res1 && $res2 && $res3 && $res4 && $res5){
            Db::commit();
            return true;
        } else {
            Db::rollback();
            return false;
        }
    }
    public function open(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $res1 = $res2 = $res3 = $res4 = $res5 = 1;
        Db::startTrans();
        $record = Db::table('tp_invest_record')->where('uid',$uid)->where('status',1)->find();
        if (empty($record)) {
            $this->retData(1, '当前没有待开放的智能云服务器');
        }
        $config = Db::table('tp_invest_config')->where('id',$record['config_id'])->find();
        $min = $config['min'];
        $balance = model('UserBalanceModel')->getBalance($uid,1);
        $ty_balance = model('UserBalanceModel')->getBalance($uid,10);

        $balance = $balance + $ty_balance;
        if ($record['close_type'] == 1) {
            if ((strtotime($record['close_date']) + 86400) > time()) {
                $this->retData(1, '关闭后需等一天后才能开启');
            }
        }
        if ($record['close_type'] == 2) {
            if ((strtotime($record['close_date']) + 86400*2) > time()) {
                $this->retData(1, '后台关闭需等两天后才能开启');
            }
        }
        if ($balance < $min) {
            $this->retData(1, '资产不满足开启条件');
        }
        
        $res1 = Db::table('tp_invest_record')->where('id',$record['id'])->update(['status'=>2,'open_date'=>date('Y-m-d H:i:s'),'open_type'=>1]);
        //自身升级
        if ($record['config_id'] >= 4) {
            if ($this->user_info['level'] == 1) {
                $res2 = Db::table('tp_user')->where('id',$uid)->update(['level'=>2,'vip_update_date'=>date('Y-m-d H:i:s')]);
            }
        }

        //上级升级
        $is_first = Db::table('tp_invest_record')->where('uid',$uid)->where('open_type','>',0)->where('config_id','>',1)->count();
        if ($is_first == 1) {
            $pid = $this->user_info['pid'];
            if ($pid != 0) {
                $p_userinfo = Db::table('tp_user')->where('id',$pid)->find();
                if ($p_userinfo['level'] < 3) {
                    $p_config_id = Db::table('tp_invest_record')->where('uid',$pid)->where('status',2)->value('config_id');
                    if ($p_config_id >= 4) {
                        $p_level = 2;
                    } else {
                        $p_level = 1; 
                    }
         
                    if ($p_userinfo['vip_next_days'] >= 10) {
                        $vip_next_days = 10;
                    } else {
                        $vip_next_days = $p_userinfo['vip_next_days'] + 1;
                    }

                    if ($p_userinfo['level'] >= 1) {
                            $res3 = Db::table('tp_user')->where('id',$pid)->update([
                                'level'=>$p_level,
                                'vip_date'=>date('Y-m-d H:i:s',strtotime($p_userinfo['vip_date'])+86400*$vip_next_days),
                                'vip_next_days'=>$vip_next_days,
                                'vip_update_date'=>date('Y-m-d H:i:s')
                            ]);
                        } else {
                            $res3 = Db::table('tp_user')->where('id',$pid)->update([
                                'level'=>$p_level,
                                'vip_date'=>date('Y-m-d H:i:s',strtotime('+5 days')),
                                'vip_next_days'=>6,
                                'vip_update_date'=>date('Y-m-d H:i:s')
                            ]);
                        }

                    //上级体验金
                    $num = Db::table('tp_invest_record')->alias('a')
                        ->join('tp_user b','b.id=a.uid and b.pid='.$pid)
                        ->where('a.config_id','>',1)
                        ->where('a.status','in','2,3,4')
                        ->group('a.uid')
                        ->count();
                    if ($num >= 3) {
                        $ty_balance = model('UserBalanceModel')->getBalance($pid,10);
                        if ($ty_balance > 0 ) {
                            if ($ty_balance > 10) {
                                $ty_balance = 10;
                            }
                            $res4 = BalanceService::updateBalance($pid,10,-$ty_balance,'体验金转出',91);
                            $res5 = BalanceService::updateBalance($pid,1,$ty_balance,'体验金转入',92);
                        }  
                    }
                }
            }
        }
        

        if($res1 && $res2 && $res3 && $res4 && $res5){
            Db::commit();
            $this->retData(0,'开启成功');
        } else {
            Db::rollback();
            $this->retData(1,'开启失败');
        }
    }


// public function test(){
//     $num = Db::table('tp_invest_record')->alias('a')
//                 ->join('tp_user b','b.id=a.uid and b.pid=1')
//                 ->where('a.config_id','>',1)
//                 ->group('a.uid')
//                 ->fetchSql(1)
//                 ->count();
//     var_dump($num);
// }

    //质押记录
    public function record(){
        $uid = $this->uid;
        $page       = P('page')?:1;
        $limit      = P('limit')?:10;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }

        $data = Db::table('tp_invest_record')->alias('a')
                // ->join('tp_invest_config b','b.id=a.config_id')
                ->where('a.uid',$uid)
                ->order('a.id desc')
                ->page($page)
                ->limit($limit)
                ->field('a.*')
                ->select();
        foreach ($data as $key => $val) {
            // $data[$key]['amount'] = $val['balance']/1;
            // $data[$key]['name'] = TranslateService::baiduApi($val['name'],$this->lang);
            // $data[$key]['order_remark'] = TranslateService::baiduApi($val['order_remark'],$this->lang);
        }
        $this->retData(0,'success',$data);
    }

     
    public function robot(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = Db::table('tp_invest_record')->where('uid',$uid)->order('id desc')->find();
        $config = Db::table('tp_invest_config')->where('id',$data['config_id'])->find();
        $data['name'] = $config['name'];
         $data['name'] = TranslateService::baiduApi($data['name'],$this->lang);
        $data['days'] = $config['days'];
        $data['rate'] = $config['rate'];
        $data['min'] = $config['min'];
        $data['max'] = $config['max'];
        $this->retData(0,'success',$data);
    }

    public function profit(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        $data['static_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',10)->sum('profit')?:0;
        $data['invite_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->sum('profit')?:0;
        $data['invite_partner_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->sum('profit')?:0;
         $data['invite_manager_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->sum('profit')?:0;
        $data['team_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',30)->sum('profit')?:0;
        $data['team_partne_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->sum('profit')?:0;
        $data['team_manager_profit'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->sum('profit')?:0;

        $this->retData(0,'success',$data);
    }


    public function profitList(){
        $uid = $this->uid;
        $page       = P('page')?:1;
        $type       = P('type')?:10;
        $limit      = P('limit')?:10;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        if (!in_array($type, [10,20,21,22,30,31,32])) {
            $this->retData(1, '类型有误');
        }
        $data = Db::table('tp_invest_profit_record')->alias('a')
                // ->join('tp_invest_record b','b.id=a.order_id')
                // ->join('tp_user c','c.id=a.x_uid','left')
                // ->where('a.uid',$uid)
                // ->whereTime('a.date','>=',date('Y-m-d'))
                ->order('a.id desc')
                ->page($page)
                ->limit($limit)
                // ->field('a.*,c.username,b.orderno')
                ->select();

        $this->retData(0,'success',$data);
    }
    

    public function tq(){
        $uid = $this->uid;
        $type = P('type')?:1;
        $amount = (float)P('amount') ?: 0;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        if (!in_array($type, [1,2])) {
            $this->retData(1, '类型有误');
        }
        if ($amount != 0) {
            if ($amount <= 0) {
                $this->retData(1, '提取数量有误');
            }
        }
        if ($type == 1) {
            $balance = model('UserBalanceModel')->getBalance($uid,11);
            $wid = 11;
        } else if ($type == 2) {
            $balance = model('UserBalanceModel')->getBalance($uid,12);
            $wid = 12;
        }

        if ($amount > 0) {
            if ($amount > $balance) {
                $this->retData(1, '余额不足');
            }
        } else {
            if ($balance <= 0) {
                $this->retData(1, '余额不足');
            }
            $amount = $balance;
        }
        Db::startTrans();
        $res1 = BalanceService::updateBalance($uid,$wid,-$amount,'佣金转出',107);
        $res2 = Db::table('tp_invest_tq')->insert([
            'uid'=>$uid,
            'amount'=>$amount,
            'date'=>date('Y-m-d H:i:s'),
            'type'=>$wid,
            'status'=>'0000'
        ]);
        if ($res1 && $res2) {
            Db::commit();
            $this->retData(0, '提取成功');
        } else {
            Db::rollback();
            $this->retData(1, '提取失败');
        }
    }


    public function getRate(){
        $data = [];
        $robot_config = Db::table('tp_invest_config')->where('id','>',0)->field('id,name')->select();
        foreach ($robot_config as $key => $val) {
            $val['name'] = TranslateService::baiduApi($val['name'],$this->lang);
            $val['rate'] = Db::table('tp_invest_rate')->whereTime('date','>=',$this->getMoothDate())->where('robot',$val['id'])->column('rate');
            array_pop($val['rate']);
            $data[] = $val;
        }
        $this->retData(0,'success',$data);
    }

    public function getProfitTotal(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        $data['robot'] = Db::table('tp_invest_record')->where('uid',$uid)->order('id desc')->value('name');
        $data['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->sum('profit');
        $data['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->whereTime('date','>=',date('Y-m-d'))->sum('profit');
        $data['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->whereTime('date','>=',$this->getMoothDate())->sum('profit');

        $this->retData(0,'success',$data);
    }

    public function getInviteTotal(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        // $data['robot'] = Db::table('tp_invest_record')->where('uid',$uid)->order('id desc')->value('name');
        $data['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->sum('profit');
        $data['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->whereTime('date','>=',date('Y-m-d'))->sum('profit');
        $data['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',20)->whereTime('date','>=',$this->getMoothDate())->sum('profit');

        $this->retData(0,'success',$data);
    }

    public function getTeamTotal(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        // $data['robot'] = Db::table('tp_invest_record')->where('uid',$uid)->where('type','in','30,31,32')->order('id desc')->value('name');
        $data['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',30)->sum('profit');
        $data['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',30)->whereTime('date','>=',date('Y-m-d'))->sum('profit');
        $data['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',30)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type','in',30)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type','in',30)->whereTime('date','>=',$this->getMoothDate())->sum('profit');

        $this->retData(0,'success',$data);
    }


    public function getRewardTotal(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data = [];
        // $data['robot'] = Db::table('tp_invest_record')->where('uid',$uid)->order('id desc')->value('name');
        $data['total'] = Db::table('tp_balance_record')->where('type',109)->where('uid',$uid)->sum('balance');
        $data['days'] = Db::table('tp_balance_record')->where('type',109)->where('uid',$uid)->whereTime('date','>=',date('Y-m-d'))->sum('balance');
        $data['yestoday'] = Db::table('tp_balance_record')->where('type',109)->where('uid',$uid)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('balance');
        $data['week'] = Db::table('tp_balance_record')->where('type',109)->where('uid',$uid)->whereTime('date','>=',$this->getWeekDate())->sum('balance');
        $data['month'] = Db::table('tp_balance_record')->where('type',109)->where('uid',$uid)->whereTime('date','>=',$this->getMoothDate())->sum('balance');

        $this->retData(0,'success',$data);
    }


    public function agentProfit(){
        $uid = $this->uid;
        if (empty($uid)) {
            $this->retData(1, '缺少参数');
        }
        $data['dlyj'] = [];
        $data['dlyj']['invite'] = [];
        $data['dlyj']['amount'] = model('UserBalanceModel')->getBalance($uid,11);
        $data['dlyj']['invite']['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->sum('profit');
        $data['dlyj']['invite']['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->whereTime('date','>=',date('Y-m-d'))->sum('profit');

        $data['dlyj']['invite']['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['dlyj']['invite']['threedays'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->whereTime('date','>=',date('Y-m-d',strtotime('-3 days')))->sum('profit');
        $data['dlyj']['invite']['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['dlyj']['invite']['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',21)->whereTime('date','>=',$this->getMoothDate())->sum('profit');
        


        $data['dlyj']['team']   = [];
        $data['dlyj']['team']['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->sum('profit');
        $data['dlyj']['team']['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->whereTime('date','>=',date('Y-m-d'))->sum('profit');

        $data['dlyj']['team']['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['dlyj']['team']['threedays'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->whereTime('date','>=',date('Y-m-d',strtotime('-3 days')))->sum('profit');
        $data['dlyj']['team']['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['dlyj']['team']['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',31)->whereTime('date','>=',$this->getMoothDate())->sum('profit');


        $data['yxjl'] = [];
        $data['yxjl']['invite'] = [];
        $data['yxjl']['team']   = [];

        $data['yxjl']['amount'] = model('UserBalanceModel')->getBalance($uid,12);
        $data['yxjl']['invite']['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->sum('profit');
        $data['yxjl']['invite']['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->whereTime('date','>=',date('Y-m-d'))->sum('profit');

        $data['yxjl']['invite']['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['yxjl']['invite']['threedays'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->whereTime('date','>=',date('Y-m-d',strtotime('-3 days')))->sum('profit');
        $data['yxjl']['invite']['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['yxjl']['invite']['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',22)->whereTime('date','>=',$this->getMoothDate())->sum('profit');
        

        $data['yxjl']['team']['total'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->sum('profit');
        $data['yxjl']['team']['days'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->whereTime('date','>=',date('Y-m-d'))->sum('profit');

        $data['yxjl']['team']['yestoday'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->whereTime('date','>=',$this->getYesDate())->whereTime('date','<=',date('Y-m-d'))->sum('profit');
        $data['yxjl']['team']['threedays'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->whereTime('date','>=',date('Y-m-d',strtotime('-3 days')))->sum('profit');
        $data['yxjl']['team']['week'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->whereTime('date','>=',$this->getWeekDate())->sum('profit');
        $data['yxjl']['team']['month'] = Db::table('tp_invest_profit_record')->where('uid',$uid)->where('type',32)->whereTime('date','>=',$this->getMoothDate())->sum('profit');
        $this->retData(0,'success',$data);
    }
}
